package com.boxuegu.caihuaxuan24.myapplication.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.boxuegu.caihuaxuan24.myapplication.R;
import com.boxuegu.caihuaxuan24.myapplication.model.LoginModel;
import com.google.gson.Gson;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;


public class RegisterActivity extends AppCompatActivity {
    TextView mBtn;
    EditText mUserName,mPasswd,mPasswd1;
    ImageView mBack;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        mBack = findViewById(R.id.back);
        mBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mUserName = findViewById(R.id.usernameedt);
        mPasswd = findViewById(R.id.passwordedt);
        mPasswd1 = findViewById(R.id.passworcomfrimdedt);
        mBtn = findViewById(R.id.registerbtn);
        mBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String username = mUserName.getText().toString().trim();
                String passwd = mPasswd.getText().toString().trim();
                String passwd1 = mPasswd1.getText().toString().trim();
                if (TextUtils.isEmpty(username)){
                    Toast.makeText(RegisterActivity.this,"用户名不能为空",Toast.LENGTH_LONG).show();
                    return;
                }
                //todo  后面两个的非空判断
                if (!passwd.equals(passwd1)){
                    Toast.makeText(RegisterActivity.this,"两次输入的密码不一致",Toast.LENGTH_LONG).show();
                    return;
                }
                RequestParams requestParams = new RequestParams("http://148.70.46.9:8080/boxuegu/bxg/register");
                requestParams.addBodyParameter("username",username);
                requestParams.addBodyParameter("password",passwd);
                x.http().post(requestParams, new Callback.CacheCallback<String>() {
                    @Override
                    public void onSuccess(String result) {
                        Gson gson = new Gson();
                        LoginModel model = gson.fromJson(result,LoginModel.class);
                        if (model.code == 0) {


                            Log.i("RegisterActivity", result);
                            finish();
                        }else {
                            Toast.makeText(RegisterActivity.this,"注册失败",Toast.LENGTH_LONG).show();
                        }
                    }

                    @Override
                    public void onError(Throwable ex, boolean isOnCallback) {
                        Log.i("RegisterActivity",ex.getMessage());
                        Toast.makeText(RegisterActivity.this,"注册失败",Toast.LENGTH_LONG).show();
                    }

                    @Override
                    public void onCancelled(CancelledException cex) {

                    }

                    @Override
                    public void onFinished() {

                    }

                    @Override
                    public boolean onCache(String result) {
                        return false;
                    }
                });


            }
        });



    }
}
